const Goods=require('../model/goods.model')
const Op = require("sequelize/lib/operators");
class GoodsService{
    async createGoodsItem(goods){
       console.log(goods)
       const res=await Goods.create(goods)
        return res.dataValues
    }
    //修稿商品信息
    async updateGoods(id,goods){
        console.log(goods)
   const res=await Goods.update(goods,{where:{id}})
        return res[0]>0?true:false

    }
    async removeItem(id){
       const res= await Goods.destroy({where:{id}})
        //返回1删除成功，0失败
        return res
    }
    async changeStore(id,offOn){
        console.log("???")
        console.log(offOn)
        const res=await Goods.update({goods_state:offOn},{where:{id}})
        console.log(res)
        return res[0]>0?true:false
    }
    async findGoods(pageNum,pageSize){
        const offset =(pageNum-1)*pageSize
        const count =await Goods.count({where:{goods_state:1}})
        const rows=await Goods. findAll({offset:offset,limit:pageSize*1,where:{
            goods_state:1
            }})
        return {
            pageNum,
            pageSize,
            total:count,
            list:rows
        }

    }
    async findOne(id){
        const res=await Goods.findOne({
            where:{
                id:id
            }
        })
        return {
            goodsInfo:res
        }
    }
    async findMyAll(user_id){
        const res=await Goods.findAll({
            where:{
                send_id:user_id
            }
            }
        )
        return res
    }
    async findSomeAllData(goods_classify,pageSize,pageNum){
        const offset =(pageNum-1)*pageSize
        const res=await Goods.findAll({
            offset:offset,
            limit:pageSize*1,
            where:{
                goods_classify:goods_classify,
                goods_state:1
            }
        })
        return res
    }
    async findListInfo(ids){
        const res=await Goods.findAll({
        where:{
            id:{
                [Op.in]:ids
                //使用Op记得引入Op
            }
        }
        })
        return res
    }
    async findMySaleOut(user_id){
        console.log(user_id)
         const res=await Goods.findAll({
             where:{
                 send_id:user_id,
                 goods_state:0
             }
         })
        return res
    }
    async findSearchGood(val){
        console.log(val)
        const res=await Goods.findAll({
            where:{
                goods_name:{
                    [Op.regexp]:val
                }
            }
        })
        console.log("??")
        return res
    }
}
module.exports=new GoodsService()